package edu.info.manager.controller;

import edu.info.manager.domain.StudentJavaBean;
import edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    Scanner scanner = new Scanner(System.in);
    StudentService Student = new StudentService();
    public void start() {

        log:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");

            int choose = scanner.nextInt();
            switch (choose) {
                case 1:
//                    System.out.println("添加学生");
                    add();
                    break;
                case 2:
//                    System.out.println("删除学生");
                    DelStudent();
                    break;
                case 3:
//                    System.out.println("修改学生");
                    UpdateStudent();
                    break;
                case 4:
//                    System.out.println("查看学生");
                    FindAllStudent();
                    break;
                case 5:
                    System.out.println("退出");
                    break log;
                default:
                    System.out.println("你的输入有误，请重新输入。");
                    break;
            }
        }
    }

    public void DelStudent() {
        String Delnum;
        while (true){

            System.out.println("请输入你要删除的Id：");
             Delnum = scanner.next();
            boolean flag = Student.isExists(Delnum);
            if(!flag)
            {
                System.out.println("你输入的ID不存在，请重新输入");

            }else
            {
                break;
            }
        } Student.DeleteStudent(Delnum);
        System.out.println("删除成功！");
    }

    public void UpdateStudent() {
        String updateid;
        while (true) {
            System.out.println("请输入学生ID:");
            updateid = scanner.next();
            boolean flag = Student.isExists(updateid);
            if (!flag) {
                System.out.println("你输入的ID不存在！");
            } else {
                break;
            }
        }
        System.out.println("请输入学生的姓名");
        String name=scanner.next();
        System.out.println("请输入学生的年龄");
        String age=scanner.next();
        System.out.println("请输入学生的生日");
        String birthday=scanner.next();
        StudentJavaBean newstu=new StudentJavaBean();
        newstu.setId(updateid);
        newstu.setAge(age);
        newstu.setName(name);
        newstu.setBirthday(birthday);
        Boolean flag=Student.UpdateStudent(updateid,newstu);
        if (flag) {
            System.out.println("修改成功");
        }else
        {
            System.out.println("修改失败");
        }
    }

    public void FindAllStudent() {
       StudentJavaBean student[]= StudentService.FindAllStudent();
       if(student==null)
       {
           System.out.println("查无信息，请添加后再试。");
           return;
       }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < student.length; i++) {
            StudentJavaBean stu=student[i];
            if(stu!=null){
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }

    public void add() {


        String id;
        while (true) {
            System.out.println("请输入学生ID:");
            id = scanner.next();
            boolean flag = Student.isExists(id);
            if (flag) {
                System.out.println("学生ID已被占用！");
            } else {
                break;
            }
        }

        System.out.println("请输入学生名字：");
        String name = scanner.next();
        System.out.println("请输入学生的年龄：");
        String age = scanner.next();
        System.out.println("请输入学生的生日：");
        String birthday = scanner.next();
        StudentJavaBean stu = new StudentJavaBean();
        stu.setId(id);
        stu.setAge(age);
        stu.setName(name);
        stu.setBirthday(birthday);
        Boolean result = Student.AddStudent(stu);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }

    }
}
